/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.settlement.service.investorsettlement;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.settlement.entity.investorsettlement.InvestorSettlement;
import com.jeesite.modules.settlement.dao.investorsettlement.InvestorSettlementDao;
import com.jeesite.modules.settlement.entity.investorsettlement.InvestorSettlementPayment;
import com.jeesite.modules.settlement.dao.investorsettlement.InvestorSettlementPaymentDao;
import com.jeesite.modules.settlement.entity.investorsettlement.InvestorSettlementPrepayment;
import com.jeesite.modules.settlement.dao.investorsettlement.InvestorSettlementPrepaymentDao;
import com.jeesite.modules.settlement.entity.investorsettlement.InvestorSettlementDetail;
import com.jeesite.modules.settlement.dao.investorsettlement.InvestorSettlementDetailDao;

/**
 * 资方结算单Service
 * @author Kilo
 * @version 2021-03-10
 */
@Service
@Transactional(readOnly=true)
public class InvestorSettlementService extends CrudService<InvestorSettlementDao, InvestorSettlement> {

	@Autowired
	private InvestorSettlementPaymentDao investorSettlementPaymentDao;

	@Autowired
	private InvestorSettlementPrepaymentDao investorSettlementPrepaymentDao;

	@Autowired
	private InvestorSettlementDetailDao investorSettlementDetailDao;

	@Autowired
	private CodeRuleService codeRuleService;

	/**
	 * 获取单条数据
	 * @param investorSettlement
	 * @return
	 */
	@Override
	public InvestorSettlement get(InvestorSettlement investorSettlement) {
		InvestorSettlement entity = super.get(investorSettlement);
		if (entity != null){
			InvestorSettlementPayment investorSettlementPayment = new InvestorSettlementPayment(entity);
			investorSettlementPayment.setStatus(InvestorSettlementPayment.STATUS_NORMAL);
			entity.setInvestorSettlementPaymentList(investorSettlementPaymentDao.findList(investorSettlementPayment));
			InvestorSettlementPrepayment investorSettlementPrepayment = new InvestorSettlementPrepayment(entity);
			investorSettlementPrepayment.setStatus(InvestorSettlementPrepayment.STATUS_NORMAL);
			entity.setInvestorSettlementPrepaymentList(investorSettlementPrepaymentDao.findList(investorSettlementPrepayment));
			InvestorSettlementDetail investorSettlementDetail = new InvestorSettlementDetail(entity);
			investorSettlementDetail.setStatus(InvestorSettlementDetail.STATUS_NORMAL);
			entity.setInvestorSettlementDetailList(investorSettlementDetailDao.findList(investorSettlementDetail));
		}
		return entity;
	}

	/**
	 * 查询分页数据
	 * @param investorSettlement 查询条件
	 * @param investorSettlement.page 分页对象
	 * @return
	 */
	@Override
	public Page<InvestorSettlement> findPage(InvestorSettlement investorSettlement) {
		return super.findPage(investorSettlement);
	}

	@Transactional(readOnly=false)
	public void saveFin(InvestorSettlement investorSettlement){
		investorSettlement.setUpdateBy(UserUtils.getUser().getLoginCode());
		investorSettlement.setUpdateDate(new Date());
		super.save(investorSettlement);
	}

	/**
	 * 保存数据（插入或更新）
	 * @param investorSettlement
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(InvestorSettlement investorSettlement) {
		if(investorSettlement.getIsNewRecord()){
			investorSettlement.setInvestorSettlementCode(codeRuleService.getCode("InvestorSettlement"));
			investorSettlement.setCreateBy(UserUtils.getUser().getLoginCode());
			investorSettlement.setCreateDate(new Date());
		}
		super.save(investorSettlement);
		// 保存 InvestorSettlement子表
		for (InvestorSettlementPayment investorSettlementPayment : investorSettlement.getInvestorSettlementPaymentList()){
			if (!InvestorSettlementPayment.STATUS_DELETE.equals(investorSettlementPayment.getStatus())){
				investorSettlementPayment.setInvestorSettlementId(investorSettlement);
				if (investorSettlementPayment.getIsNewRecord()){
					investorSettlementPaymentDao.insert(investorSettlementPayment);
				}else{
					investorSettlementPaymentDao.update(investorSettlementPayment);
				}
			}else{
				investorSettlementPaymentDao.delete(investorSettlementPayment);
			}
		}
		// 保存 InvestorSettlement子表
		for (InvestorSettlementPrepayment investorSettlementPrepayment : investorSettlement.getInvestorSettlementPrepaymentList()){
			if (!InvestorSettlementPrepayment.STATUS_DELETE.equals(investorSettlementPrepayment.getStatus())){
				investorSettlementPrepayment.setInvestorSettlementId(investorSettlement);
				if (investorSettlementPrepayment.getIsNewRecord()){
					investorSettlementPrepaymentDao.insert(investorSettlementPrepayment);
				}else{
					investorSettlementPrepaymentDao.update(investorSettlementPrepayment);
				}
			}else{
				investorSettlementPrepaymentDao.delete(investorSettlementPrepayment);
			}
		}
		// 保存 InvestorSettlement子表
		for (InvestorSettlementDetail investorSettlementDetail : investorSettlement.getInvestorSettlementDetailList()){
			if (!InvestorSettlementDetail.STATUS_DELETE.equals(investorSettlementDetail.getStatus())){
				investorSettlementDetail.setInvestorSettlementId(investorSettlement);
				if (investorSettlementDetail.getIsNewRecord()){
					investorSettlementDetailDao.insert(investorSettlementDetail);
				}else{
					investorSettlementDetailDao.update(investorSettlementDetail);
				}
			}else{
				investorSettlementDetailDao.delete(investorSettlementDetail);
			}
		}
	}

	/**
	 * 更新状态
	 * @param investorSettlement
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(InvestorSettlement investorSettlement) {
		super.updateStatus(investorSettlement);
	}

	/**
	 * 删除数据
	 * @param investorSettlement
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(InvestorSettlement investorSettlement) {
		super.delete(investorSettlement);
		InvestorSettlementPayment investorSettlementPayment = new InvestorSettlementPayment();
		investorSettlementPayment.setInvestorSettlementId(investorSettlement);
		investorSettlementPaymentDao.deleteByEntity(investorSettlementPayment);
		InvestorSettlementPrepayment investorSettlementPrepayment = new InvestorSettlementPrepayment();
		investorSettlementPrepayment.setInvestorSettlementId(investorSettlement);
		investorSettlementPrepaymentDao.deleteByEntity(investorSettlementPrepayment);
		InvestorSettlementDetail investorSettlementDetail = new InvestorSettlementDetail();
		investorSettlementDetail.setInvestorSettlementId(investorSettlement);
		investorSettlementDetailDao.deleteByEntity(investorSettlementDetail);
	}
}
